﻿using Koala.Pro.Common;
using System.Globalization;
using System.Windows.Data;

namespace Koala.Pro.Helpers
{
    public class NumberToYesNoStrConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool? flag = ConvertToBoolean(value);
            if (!flag.HasValue)
            {
                return "";
            }
            if (flag != true)
            {
                return "否";
            }
            return "是";
        }

        private bool? ConvertToBoolean(object value)
        {
            if (value == null || value.ToString().IsNullOrEmpty())
            {
                return null;
            }
            bool value2;
            if (!(value is float num))
            {
                if (!(value is double num2))
                {
                    if (!(value is int num3))
                    {
                        if (!(value is long num4))
                        {
                            throw new NotSupportedException($"value must be a number type, got {value?.GetType()}");
                        }
                        value2 = (float)num4 != 0f;
                    }
                    else
                    {
                        value2 = num3 != 0;
                    }
                }
                else
                {
                    value2 = num2 != 0.0;
                }
            }
            else
            {
                value2 = num != 0f;
            }
            return value2;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || value.ToString().IsNullOrEmpty())
            {
                return DBNull.Value;
            }
            return (!(value.ToString() == "是")) ? 1 : 0;
        }
    }
}
